// pages/userset/password/password.js
const api = require('../../../utils/interface.js')
const math = require("../../../utils/math.js")
const tools = require('../../../utils/util.js')
const md5 = require('../../../utils/md5.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ajxtrue: false,
    password: "",
    msgId: "",
    checkword: "",
    code: "发送验证码",
    iscode: "", //用于存放验证码接口里获取到的code
    id: ""
  },
  // 密码验证
  blurPhone: function(e) {
    var password = e.detail.value;
    let that = this
    if (!(/^\d{6}$/.test(password))) {
      this.setData({
        ajxtrue: false,
      })
      if (password.length >= 6) {
        wx.showToast({
          title: '密码为6位数字',
          icon: 'none',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true,
        password: password
      })
      console.log('验证成功', that.data.ajxtrue)
    }
  },
  // 密码验证
  blurPhone1: function(e) {
    var checkword = e.detail.value;
    this.setData({
      checkword: checkword
    })
  },
  //验证码
  blurPhone2(e) {
    var code = e.detail.value;
    this.setData({
      iscode: code
    })
  },
  //获取验证码
  getVerificationCode() {
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },

  getCode: function() {
    var _this = this
    api.getCode({
      phone: this.data.phone
    }).then(res => {
      console.log(res, "res")
      _this.setData({
        msgId: res.data.result
      })
      var num = 61;
      var timer = setInterval(function() {
        num--;
        if (num <= 0) {
          clearInterval(timer);
          _this.setData({
            code: '重新发送',
            disabled: false
          })

        } else {
          _this.setData({
            code: num + "s"
          })
        }
      }, 1000)
    })


  },
  // 表单提交
  formSubmit(e) {
    let that = this
    let val = e.detail.value
    let ajxtrue = this.data.ajxtrue
    if (this.data.password == "" || this.data.checkword == "") {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.iscode == "") {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (this.data.password !== this.data.checkword) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (ajxtrue == true) {
      api.updatePayPassword({
        payPassword: md5.md5(this.data.password).toLowerCase(),
        phone: this.data.phone,
        code: this.data.iscode,
        id: this.data.id,
        msgId: this.data.msgId
      }).then(res => {
        if (res.data.status == 200) {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/member/member',
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.code,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '密码必须为6位数字',
        icon: 'success',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let user = wx.getStorageSync('userInfo').phone
    this.data.id = options.id
    this.setData({
      phone: user
    })
  },
})